﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using ZhuXingFamilys.ExportExcel.entity;

namespace ZhuXingFamilys.ExportExcel.utils
{
    /// <summary>
    /// 点击类型树生成excelData数据工具
    /// </summary>
    public class excelDataByCategoryTree
    {
        ObservableCollection<excelData> list;
        DefinitionNode dfi = new DefinitionNode();
        public excelDataByCategoryTree(ObservableCollection<excelData> _list, DefinitionNode _dfi) 
        {
            list = _list.Copy();
            this.dfi = _dfi;
        }
        public excelDataByCategoryTree(ObservableCollection<excelData> _list)
        {
            list = _list.Copy();
        }

        // 全部
        public ObservableCollection<excelData> getAll() 
        {
            ObservableCollection<excelData> excelListByCategory = new ObservableCollection<excelData>();
            if (list == null)
                return excelListByCategory;
            int id = 1;
            foreach (excelData excel in list)
            {
                excel.Id = id;
                if (excelListByCategory.Count != 0)
                {
                    bool contains = false;
                    foreach (excelData excel1 in excelListByCategory)
                    {
                        try
                        {
                            //if (excel1.TypeId.Equals(excel.TypeId) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            if (excel1.Name.Equals(excel.Name) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            {
                                contains = true;
                                excel1.elementId = excel1.elementId + "," + excel.elementId;
                                excel1.Volume = Math.Round(excel1.Volume + excel.Volume, 3); //保留三位小数
                                excel1.Area = Math.Round(excel1.Area + excel.Area, 3);
                                excel1.Length = Math.Round(excel1.Length + excel.Length, 3);
                                //excel1.Volume += excel.Volume;
                                //excel1.Area += excel.Area;
                                //excel1.Length += excel.Length;
                                excel1.Num += excel.Num;
                                excel1.NumCount++;
                                break;
                            }
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                    if (!contains)
                    {
                        excelListByCategory.Add(excel);
                        id++;
                    }
                }
                else
                {
                    excelListByCategory.Add(excel);
                    id++;
                }

            }
            return excelListByCategory;
        }
        // 点击专业
        public ObservableCollection<excelData> getForSpecialty()
        {
            ObservableCollection<excelData> excelListByCategory = new ObservableCollection<excelData>();
            if (list == null)
                return excelListByCategory;
            int id = 1;
            foreach (excelData excel in list)
            {
                if (excel.SpecialitieId == int.Parse(dfi.SpecialitieId))
                {
                    //excelListByCategory.Add(excel);
                    excel.Id = id;
                    if (excelListByCategory.Count != 0)
                    {
                        bool contains = false;
                        foreach (excelData excel1 in excelListByCategory)
                        {
                            if (excel1.Name.Equals(excel.Name) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            {
                                contains = true;
                                excel1.elementId = excel1.elementId + "," + excel.elementId;
                                excel1.Volume = Math.Round(excel1.Volume + excel.Volume, 3); //保留三位小数
                                excel1.Area = Math.Round(excel1.Area + excel.Area, 3);
                                excel1.Length = Math.Round(excel1.Length + excel.Length, 3);
                                excel1.Num += excel.Num;
                                excel1.NumCount++;
                                break;
                            }
                        }
                        if (!contains)
                        {
                            excelListByCategory.Add(excel);
                            id++;
                        }
                    }
                    else
                    {
                        excelListByCategory.Add(excel);
                        id++;
                    }
                }
            }
            return excelListByCategory;
        }
        // 点击详细类型
        public ObservableCollection<excelData> getForDetailType()
        {
            ObservableCollection<excelData> excelListByCategory = new ObservableCollection<excelData>();
            if (list == null)
                return excelListByCategory;
            int id = 1;
            foreach (excelData excel in list)
            {
                if (excel.SpecialitieId == int.Parse(dfi.SpecialitieId) && excel.Category == dfi.Name)
                {
                    //excelListByCategory.Add(excel);
                    excel.Id = id;
                    if (excelListByCategory.Count != 0)
                    {
                        bool contains = false;
                        foreach (excelData excel1 in excelListByCategory)
                        {
                            if (excel1.Name.Equals(excel.Name) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            {
                                contains = true;
                                excel1.elementId = excel1.elementId + "," + excel.elementId;
                                excel1.Volume = Math.Round(excel1.Volume + excel.Volume, 3); //保留三位小数
                                excel1.Area = Math.Round(excel1.Area + excel.Area, 3);
                                excel1.Length = Math.Round(excel1.Length + excel.Length, 3);
                                excel1.Num += excel.Num;
                                excel1.NumCount++;
                                break;
                            }
                        }
                        if (!contains)
                        {
                            excelListByCategory.Add(excel);
                            id++;
                        }
                    }
                    else
                    {
                        excelListByCategory.Add(excel);
                        id++;
                    }
                }
            }
            return excelListByCategory;
        }
        // 点击详细楼层
        public ObservableCollection<excelData> getForDetailedElevation()
        {
            ObservableCollection<excelData> excelListByCategory = new ObservableCollection<excelData>(); 
            if (list == null)
                return excelListByCategory;
            int id = 1;
            foreach (excelData excel in list)
            {
                if (excel.SpecialitieId == int.Parse(dfi.SpecialitieId) && dfi.Name.Equals(excel.Elevation))
                {
                    excel.Id = id;
                    if (excelListByCategory.Count != 0)
                    {
                        bool contains = false;
                        foreach (excelData excel1 in excelListByCategory)
                        {
                            if (excel1.Name.Equals(excel.Name) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            {
                                contains = true;
                                excel1.elementId = excel1.elementId + "," + excel.elementId;
                                excel1.Volume = Math.Round(excel1.Volume + excel.Volume, 3); //保留三位小数
                                excel1.Area = Math.Round(excel1.Area + excel.Area, 3);
                                excel1.Length = Math.Round(excel1.Length + excel.Length, 3);
                                excel1.Num += excel.Num;
                                excel1.NumCount++;
                                break;
                            }
                        }
                        if (!contains)
                        {
                            excelListByCategory.Add(excel);
                            id++;
                        }
                    }
                    else
                    {
                        excelListByCategory.Add(excel);
                        id++;
                    }
                }
            }
            return excelListByCategory;
        }
        // 点击“类型、楼层”
        public ObservableCollection<excelData> getOther()
        {
            ObservableCollection<excelData> excelListByCategory = new ObservableCollection<excelData>();
            if (list == null)
                return excelListByCategory;
            int id = 1;
            foreach (excelData excel in list)
            {
                if (excel.SpecialitieId == int.Parse(dfi.SpecialitieId))
                {
                    //excelListByCategory.Add(excel);
                    excel.Id = id;
                    if (excelListByCategory.Count != 0)
                    {
                        bool contains = false;
                        foreach (excelData excel1 in excelListByCategory)
                        {
                            if (excel1.Name.Equals(excel.Name) && excel1.FamilyNumber.Equals(excel.FamilyNumber))
                            {
                                contains = true;
                                excel1.elementId = excel1.elementId + "," + excel.elementId;
                                excel1.Volume = Math.Round(excel1.Volume + excel.Volume, 3); //保留三位小数
                                excel1.Area = Math.Round(excel1.Area + excel.Area, 3);
                                excel1.Length = Math.Round(excel1.Length + excel.Length, 3);
                                excel1.Num += excel.Num;
                                excel1.NumCount++;
                                break;
                            }
                        }
                        if (!contains)
                        {
                            excelListByCategory.Add(excel);
                            id++;
                        }
                    }
                    else
                    {
                        excelListByCategory.Add(excel);
                        id++;
                    }
                }
            }
            return excelListByCategory;
        }
    }
}
